﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Collections.Specialized;

namespace com.dz.ct.common
{
    public class AppConfig
    {
        /// <summary>
        /// sql连接字符串
        /// </summary>
        public static string SqlConnectionString
        {
            get { return GetConfig("SqlConnectionString", @"Data Source=.;Initial Catalog=BookInstance;User ID=sa;Password=123456"); }
        }

        /// <summary>
        /// 存放书章节的根目录
        /// </summary>
        public static string BookChapterRoot
        {
            get
            {
                string t_path = string.Empty;
                if (string.IsNullOrEmpty(GetConfig("BookChapterRoot", "")))
                {
                    t_path = AppDomain.CurrentDomain.BaseDirectory;
                }
                else
                {
                    t_path = GetConfig("BookChapterRoot", "");
                }
                return t_path;
            }
        }

        /// <summary>
        /// 存放图片封面根目录
        /// </summary>
        public static string BookImgRoot
        {
            get
            {
                string t_path = string.Empty;
                if (string.IsNullOrEmpty(GetConfig("BookImgRoot", "")))
                {
                    t_path = AppDomain.CurrentDomain.BaseDirectory;
                }
                else
                {
                    t_path = GetConfig("BookImgRoot", "");
                }
                return t_path;
            }
        }

        /// <summary>
        /// 抓取书列表最大多少页
        /// </summary>
        public static int MaxBookListPageCount
        {
            get
            {
                int t_max_count = 10;
                if (!int.TryParse(GetConfig("MaxBookListPageCount", "10"), out t_max_count))
                    t_max_count = 10;
                return t_max_count;
            }
        }

        /// <summary>
        /// 抓取书列表最大多少页
        /// </summary>
        public static int startPageIndex
        {
            get
            {
                int t_start_count = 1;
                if (!int.TryParse(GetConfig("startPageIndex", "1"), out t_start_count))
                    t_start_count = 1;
                return t_start_count;
            }
        }

        /// <summary>
        /// 抓取书籍章节模式 -1 全部冲洗抓取 0 从上次记录下的地方开始抓取
        /// </summary>
        public static int CatchBookChapterMode
        {
            get
            {
                int t_mode = -1;
                if (!int.TryParse(GetConfig("CatchBookChapterMode", "-1"), out t_mode))
                    t_mode=-1;
                return t_mode;
            }
        }

        /// <summary>
        /// 升序或降序方式每次去多少条记录去更新
        /// </summary>
        public static int PerRowCount
        {
            get
            {
                int t_row_count = 10;
                if (!int.TryParse(GetConfig("PerRowCount", "10"), out t_row_count))
                    t_row_count = 10;
                return t_row_count;
            }
        }

        /// <summary>
        /// -1 全部书籍一起更新 0 从上次更新的地方继续往下更新
        /// </summary>
        public static int CatchType
        {
            get
            {
                int t_catch_type = 0;
                if (!int.TryParse(GetConfig("CatchType", "0"), out t_catch_type))
                    t_catch_type = 10;
                return t_catch_type;
            }
        }

        /// <summary>
        /// 是否启用图片和章节备份
        /// </summary>
        public static bool EnableBakUp
        {
            get
            {
                bool t_bak = false;
                if(!bool.TryParse(GetConfig("EnableBakUp","false"),out t_bak))
                    t_bak=false;
                return t_bak;
            }
        }

        /// <summary>
        /// db轮换方式每次等待时间 单位小时
        /// </summary>
        public static int DbWaitTime
        {
            get
            {
                int t_wait_time = 12;
                if(!int.TryParse(GetConfig("DbWaitTime","12"),out t_wait_time))
                    t_wait_time=12;
                return t_wait_time;
            }
        }

        /// <summary>
        /// 收集的站点列表
        /// </summary>
        public static string SiteNameList
        {
            get
            {
                return GetConfig("SiteNameList", "site_3");
            }
        }

        /// <summary>
        /// 抓取的站点
        /// </summary>
        public static int CatchSite
        {
            get
            {
                int t_site = -1;
                if (!int.TryParse(GetConfig("CatchSite", "-1"), out t_site))
                    t_site = -1;
                return t_site;
            }
        }

        /// <summary>
        /// 开启db收集
        /// </summary>
        public static bool EnableDb
        {
            get
            {
                bool t_enable_db = false;
                if (!bool.TryParse(GetConfig("EnableDb", "false"), out t_enable_db))
                    t_enable_db = false;
                return t_enable_db;
            }
        }
      
        /// <summary>
        /// 线程数量
        /// </summary>
        public static int ThreadCount
        {
            get
            {
                int t_thread_count = 0;
                if (!int.TryParse(GetConfig("ThreadCount", "10"), out t_thread_count))
                    t_thread_count = 10;
                return t_thread_count;
            }
        }




        /// <summary>
        /// 系统配置文件路径
        /// </summary>
        public static string SysConfigFile
        {
            get
            {
                string t_path = string.Empty;
                if (string.IsNullOrEmpty(GetConfig("SysConfigFile", "")))
                {
                    t_path =System.IO.Path.Combine( AppDomain.CurrentDomain.BaseDirectory,"sys_config.config");
                }
                else
                {
                    t_path = GetConfig("SysConfigFile", "");
                }
                return t_path;
            }
        }
        #region tools
        private static NameValueCollection _settings = null;

        public static NameValueCollection Settings
        {
            get { return AppConfig._settings; }
            set { AppConfig._settings = value; }
        }



        private static string GetConfig(string key, string defaultValue)
        {

            if (_settings == null)
                _settings = ConfigurationManager.AppSettings;

            if (Settings[key] != null)
                return Settings[key];
            else
                return defaultValue;

        }
        #endregion
    }
}
